package dao;

import org.apache.ibatis.annotations.*;
import pojo.Room;

import java.util.List;

@Mapper
public interface RoomMapper {

    @Select("SELECT * FROM room")
    @Results({
            @Result(column = "room_id", property = "roomId"),
            @Result(column = "room_name", property = "roomName"),
            @Result(column = "floor", property = "floor"),
            @Result(column = "depart_id1", property = "departId1"),
            @Result(column = "depart_id2", property = "departId2")
    })
    List<Room> findAll();

    @Select("SELECT * FROM room WHERE room_id = #{roomId}")
    @Results({
            @Result(column = "room_id", property = "roomId"),
            @Result(column = "room_name", property = "roomName"),
            @Result(column = "floor", property = "floor"),
            @Result(column = "depart_id1", property = "departId1"),
            @Result(column = "depart_id2", property = "departId2")
    })
    Room findById(int roomId);

    @Insert("INSERT INTO room(room_name, floor, depart_id1, depart_id2) " +
            "VALUES(#{roomName}, #{floor}, #{departId1}, #{departId2})")
    @Options(useGeneratedKeys = true, keyProperty = "roomId")
    int addRoom(Room room);

    @Update("UPDATE room SET " +
            "room_name = #{roomName}, " +
            "floor = #{floor}, " +
            "depart_id1 = #{departId1}, " +
            "depart_id2 = #{departId2} " +
            "WHERE room_id = #{roomId}")
    int updateRoom(Room room);

    @Delete("DELETE FROM room WHERE room_id = #{roomId}")
    int deleteRoom(int roomId);

    @Select("SELECT * FROM room WHERE depart_id1 = #{departId1}")
    @Results({
            @Result(column = "room_id", property = "roomId"),
            @Result(column = "room_name", property = "roomName"),
            @Result(column = "floor", property = "floor"),
            @Result(column = "depart_id1", property = "departId1"),
            @Result(column = "depart_id2", property = "departId2")
    })
    List<Room> findByDepartId1(int departId1);
}